// Copyright (C) 1995 Steve Hayman
// Use is governed by the MiscKit license

#import "MiscShell.subproj/MiscShell.h"
#import "MiscShellInspector.h"
#import "EmacsText.h"

@implementation MiscShellInspector
// Simple view swapping methods in response to the popup
//
- swapToBox:aBox
{
    [swapBox setContentView:[aBox contentView]];
    [swapBox display];
    return self;
}
// the popup buttons send this
- swapView:sender
{
    switch( [[sender selectedCell] tag] ) {
    case 0:	
	[self swapToBox:scriptBox ]; 
	break;
    case 1:	
	[self swapToBox:optionsBox]; 
	break;
    }
    
    return self;
}

// Load the Inspector.
// TODO - replace this with an EmacsText object

- init
{
    char buf[MAXPATHLEN + 1];
    id bundle;
    NXRect textFrame;
    Font *originalFont;
    
    [super init];
    
    bundle = [NXBundle bundleForClass:[MiscShell class]];
    [bundle getPath: buf forResource: "MiscShellInspector" ofType:"nib"];
    [NXApp loadNibFile:buf owner:self withNames:NO fromZone:[self zone]];
    
    [self swapToBox:scriptBox];
    /*
     * at this point, "text" is pointing to the old scrolling text.
     */
    originalFont = [text font];

    /*
     * Now we want to create an EmacsText object and replace the text
     * inside the "document" scroll view with it.
     * Some of this code is borrowed from Scott Anguish's enhanced Yap.
     * Thanks, Scott.
     */
    [[scrollView docView] getFrame:&textFrame];
    text = [[EmacsText alloc] 
	initFrame:&textFrame text:"" alignment:NX_LEFTALIGNED];
    /*
     * Put this new text object in the window and free the IB-created one.
     */
    [[scrollView setDocView:text] free];
    
    /*
     * Finish setting up the new text object
     */
    [text setFont:originalFont];		// Whatever was set in IB

    [text setVertResizable:YES];		// Grow down as you type
    [text setHorizResizable:NO];		// But not sideways 
    [text setAutosizing:NX_WIDTHSIZABLE];	// Size horizontally when resized
    [text setMonoFont:YES];
    [text setOpaque:YES];
    [text setMinSize:&textFrame.size];
    NX_WIDTH(&textFrame) = NX_HEIGHT(&textFrame) = 1.0e38;
    [text setMaxSize:&textFrame.size];	// Can grow
    [text setSel:0:0];			// Set the selection
    [text setDelegate:self];
    [text sizeToFit];
		   
    return self;
}


/*
 * revert  - set inspector to reflect object's state
 */
- revert: sender
{
    int tag;
    [text setText: [[object script] stringValue] ];
    [runToCompletionCheck setState:[object runToCompletion]];
    switch( [object delimiter] ) {
    case 0:	tag = 0; break;
    case '\t':	tag = 1; break;
    case ' ':	tag = 2; break;
    case ':':	tag = 3; break;
    default:	tag = 0; break;	// ??? TODO - print an error message
    }
    
    [delimiters selectCellWithTag:tag];
    [customDelimiters setStringValue: [[object customDelimiters] stringValue]];
    [tableSort selectCellWithTag: [object sortWhenColumnsMove]];
    return [super revert:sender];
}


/*
 * ok - set object to reflect inspector's contents.  various objects in
 * the inspector panel send us this message when they're clicked.
 *
 */
- ok: sender
{
    MiscString *newScript = [[MiscString alloc] init];
    MiscString *aLine = [[MiscString alloc] init];
    NXStream *textStream = [text stream];
    char newDelim;
    MiscString *newDelimiters = [[MiscString alloc] init];
    
    [self touch:self];			// mark inspector panel edited
    [[NXApp activeDocument] touch];	// mark nib as edited
    /*
     * The textStream isn't necessarily at the start when we do [text stream];
     * it might be where we left it before.  which could cause us to
     * hit EOS immediately.  which did happen.  which drove me nuts for
     * a while as my carefully setup scripts kept vanishing in the inspector.
     */
    NXSeek(textStream, 0L, NX_FROMSTART);
    
    while ( [aLine streamGets:textStream] != EOF) {
	[newScript concatenate:aLine];
    }
    [newScript concatenate:aLine];	// last incomplete line if any

    [object setScript:newScript];	// it makes a copy
   
    [newDelimiters setStringValue: [customDelimiters stringValue]];
    [object setCustomDelimiters:newDelimiters];	// it makes a copy
    [newDelimiters free];
    
    [newScript free];
    [aLine free];
    
    [object setRunToCompletion: [runToCompletionCheck state]];
    
    switch ( [[delimiters selectedCell] tag] ) {
    case 0:	newDelim = 0; break;
    case 1:	newDelim = '\t'; break;
    case 2:	newDelim = ' '; break;
    case 3:	newDelim = ':'; break;
    default:	newDelim = 0; break;	// ??? todo - add an error msg
    }
    
    [object setDelimiter:newDelim];
    [object setSortWhenColumnsMove: [[tableSort selectedCell] tag]];
    return self;
}
- (BOOL)wantsButtons
{
    return NO;
}

// Text delegate methods - we're the delegate of the script text, so we
// want to do "ok" whenever the text changes.
// thanks to the StringList palette for this clever method.
- textDidGetKeys:sender isEmpty:(BOOL)flag 
{
    [self perform:@selector(ok:) with:self afterDelay:500 cancelPrevious:YES];
    return self; 
}

// same deal for text fields
- textDidChange:sender
{
    [self perform:@selector(ok:) with:self afterDelay:500 cancelPrevious:YES];
    return self;
}

@end
